Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Brodes/seh flow phase1 throwing models #18014

Open
wants to merge 22 commits into
base: main
Choose a base branch
from

Conversation

bdrodes
Copy link
Contributor

@bdrodes bdrodes commented Nov 18, 2024

Part of a phased approach to an overhaul of how exceptions are modeled and handled in IR, in order to handle both SEH (structured exception handling) and ordinary C++ exceptions. These two types of exceptions have subtly different mechanics and uses. Consequently, users cannot treat all exceptions as the same. E.g., a call to memset can raise an SEH exception but it would not raise an exception with C++ exceptions.

In this PR, I start by creating a new Throwing.qll library to provide metadata for any kind of exception, and a predicate to indicate if it is SEH or CXX. This change impacts models for NonThrowingFunctions, such as (as previously mentioned) memset. This PR, however, does not fully provide support to differentiate SEH and CXX exception handling in the IR. This will be addressed in future PRs.

@bdrodes bdrodes marked this pull request as ready for review November 19, 2024 15:46
@bdrodes bdrodes requested a review from a team as a code owner November 19, 2024 15:46
*/
abstract predicate mayThrowException(boolean unconditional);
final predicate mayRaiseException() { this.raisesException(false) }
Copy link
Contributor

@jketema jketema Nov 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, the mayThrowException predicate will need to be kept for now and marked as deprecated.

Copy link
Contributor Author

@bdrodes bdrodes Nov 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to rethink this generally for backwards compatibility given the original thing being deprecated is abstract. Stand by...

Copy link
Contributor Author

@bdrodes bdrodes Nov 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I decided to move back to the old mechanic. I switched because SEH doesn't really throw an exception per se. But it's fine, and easier to just keep it the way it was. The changes have been made, just waiting for the checks to pass.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm seeing failures with some security/CWE tests, but I cannot recreate that locally. @MathiasVP is that a common discrepancy? can you maybe run the test locally as well? Thye all passed for me.

@paldepind
Copy link
Contributor

Why do we need to deprecate NonThrowingFunction and add a new NonThrowingFunction that extends ExceptionAnnotation? To me it seems that the things in ExceptionAnnotation are not relevant for NonThrowingFunction? E.g., having a getExceptionType on a function that doesn't throw exceptions.

Perhaps the stuff in ExceptionAnnotation could be moved inside ThrowingFunction, the ExceptionAnnotation could be removed, and NonThrowingFunction could be kept as is. Would that not work?

@MathiasVP
Copy link
Contributor

MathiasVP commented Nov 20, 2024

Why do we need to deprecate NonThrowingFunction and add a new NonThrowingFunction that extends ExceptionAnnotation? To me it seems that the things in ExceptionAnnotation are not relevant for NonThrowingFunction? E.g., having a getExceptionType on a function that doesn't throw exceptions.

This is because we want to distinguish between the kinds of exceptions that cannot be thrown. For example, memcpy cannot throw any C++ exceptions. However, when running on a Windows machine it may throw a structured exception (for example, if memcpy dereferences an invalid pointer). So we need to be able to distinguish between "a function that cannot throw a C++ exception, but may throw a structured exception" and "a function that may throw a C++ exception".

I don't care specifically about how we distinguish between those two concepts. I agree that it's a bit unintuitive that you implement a getExceptionType predicate when extending NonThrowing. The interpretation is that this models that the function cannot throw an exception of this specific type (where "type" here refers to whether it's a structured exception or a C++ exception).

Other API suggestions could be:

  1. 4 classes: NonThrowingSehException, NonThrowingCppException, ThrowingSehException, ThrowingCppException (where, for the last two classes, you'd need to implement a predicate that specifies whether this is a "must" or a "may" throw).
  2. Get rid of NonThrowing and Throwing and simply have one class ExceptionFunction (similar to the one in this PR which is named ExceptionAnnotation) where you implement a predicate like hasException(Exception e, Option<Boolean>::Option unconditional) (or any other three-valued type) with the specification that:
    • A function may throw a C++ exception if hasException(TCxxException(), Option<Boolean>::some(false)) holds
    • A function may throw a SEH exception if hasException(TSehException(), Option<Boolean>::some(false)) holds (this one isn't really needed since any function should be assumed to may throw a SEH exception inside a try block)
    • A function must throw a C++ exception if hasException(TCxxException(), Option<Boolean>::some(true)) holds
    • A function must throw a SEH exception if hasException(TSehException(), Option<Boolean>::some(true)) holds
    • A function will not throw a C++ exception if hasException(TCxxException(), Option<Boolean>::none()) holds
    • A function will not throw a SEH exception if hasException(TSehException(), Option<Boolean>::none()) holds (I don't imagine this will ever be necessary as this guarantee cannot really be made about any function)

@@ -106,6 +106,8 @@ private class MemcpyFunction extends ArrayFunction, DataFlowFunction, SideEffect
not this.hasGlobalName(["bcopy", mempcpy(), "memccpy"]) and
index = this.getParamDest()
}

override TCxxException getExceptionType() { any() }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find this confusing. Together with the NonThrowing this seems to say that:

This function is non-throwing, but only in the case of C++ exceptions. So it may still throw a SEH exception.

Is that the correct reading?

Copy link
Contributor Author

@bdrodes bdrodes Nov 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's right. That's what it is saying. It is no longer sufficient to say a function doesn't throw, you have to say how it doesn't throw (which kind of exception doesn't it throw). If they want to say it doesn't throw any you can just return the parent exception type.

The issue we got into with making memcpy nonthrowing is that it is true that it doesn't throw a C++ exception, but it absolutely throws a SEH exception. The mechanics in this PR force users to think about what it is they really want when they say a function throws or doesn't throw.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.

I wonder if this is all somewhat overly complicated. In my understanding the following cases are interesting:

  • C++ exceptions:
    • Functions that may throw (this is the default, but are modelling of this in the IR is limited)
    • Functions that do never throw a C++ exception, i.e., functions marked either as `noexcept or that are C functions (that we model)
  • SEH exceptions:
    • Functions that may throw (this is the default, and we want to improve the modelling in the IR here)
    • Functions that always throw

Is this correct?

Copy link
Contributor

@jketema jketema Nov 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a brief meeting. The conclusion was that my assessment above is correct. The proposal is do something simpler:

  • Rename, NonThrowing to something like NonCppThrowingFunction and introduce a deprecated NonThrowing alias.
  • Introduce a new class AlwaysSehThrowingFunction which is used to model functions that always throw an SEH exception.
  • Deprecate the Throwing class.

We should also remove the use of the Throwing class in the models, but we can only do that in the next phase when we update the IR (otherwise the IR breaks).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overhauled the PR, let me know if that works.

Copy link
Contributor

@paldepind paldepind Nov 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Introduce a new class AlwaysSehThrowingFunction which is used to model functions that always throw an SEH exception.

@jketema When you say "always throw an SEH exception" does that mean that the function will always throw an SEH exception when called or that the function can only throw an SEH exception but doesn't necessarily always do so? To me the name sounds like the former but I guess it's actually the latter?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The former: the function will always throw an SEH exception

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I see. So I guess it's for functions whose purpose is to raise exceptions? Like something along the lines of throw_my_SEH_exception?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes: RaiseException, ExRaiseAccessViolation, ExRaiseDatatypeMisalignment.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, thanks :)

Copy link
Contributor

@jketema jketema left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some small comments, otherwise LGTM.

Comment on lines +9 to +10
* A function that is guaranteed to never throw a C++ exception
* (distinct from a structured exception handling, SEH, exception).
Copy link
Contributor

@jketema jketema Nov 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* A function that is guaranteed to never throw a C++ exception
* (distinct from a structured exception handling, SEH, exception).
* A function that is guaranteed to never throw a C++ exception.
*
* The function may still raise a structured exception handling (SEH) exception.

* A class that models the exceptional behavior of a function.
* A function that is known to raise an exception.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would leave this as it was, as we're going to deprecate this anyway.

Comment on lines +17 to +18
ThrowingFunction() { any() }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem necessary?

Comment on lines +26 to +30
/**
* A function that is known to raise an exception unconditionally.
* The only cases known where this happens is for SEH
* (structured exception handling) exceptions.
*/
Copy link
Contributor

@jketema jketema Nov 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since "SEH" is in the name of the class, I would change this to just:

Suggested change
/**
* A function that is known to raise an exception unconditionally.
* The only cases known where this happens is for SEH
* (structured exception handling) exceptions.
*/
/**
* A function that unconditionally raises a structured exception handling (SEH) exception.
*/

Comment on lines 365 to 369
final override predicate mayThrowException() {
expr.getTarget().(ThrowingFunction).mayThrowException(_)
or
expr.getTarget() instanceof AlwaysSehThrowingFunction
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should be able to remove this predicate completely, but let's leave that as a follow up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants